package com.baiduspring.utils.security;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util implements BaseSecurity {
    private static Md5Util ourInstance = new Md5Util();

    public static Md5Util getInstance() {
        return ourInstance;
    }

    private Md5Util() {
    }

    public String encode(String code) {
        MessageDigest md5;

        StringBuffer encode = new StringBuffer();
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] byteArray;
            byteArray = code.getBytes("UTF-8");
            byte[] md5Bytes = md5.digest(byteArray);

            forencode(encode, md5Bytes);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return encode.toString();
    }
}
